package com.inno.subject.domain.utils;

import com.inno.subject.common.error.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.pdfbox.io.RandomAccessBuffer;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;

@Slf4j
public class CommonUtil {
    /**
     * 计算比率
     *
     * @param total 总数
     * @param num   数量
     * @return 比率
     */
    public static BigDecimal calcRate(int total, int num) {
        if (0 == total || 0 == num){
            return BigDecimal.ZERO;
        }
        return BigDecimal.valueOf(num).divide(BigDecimal.valueOf(total), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
    }

    /**
     * 读取word
     */
    public static String readWord(InputStream is) {
        try (is; XWPFDocument doc = new XWPFDocument(is)) {
            XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
            return extractor.getText();
        } catch (IOException e) {
            log.error("读取文件失败", e);
            throw new BusinessException("解析简历文件失败，请重试");
        }


    }

    /**
     * 读取pdf
     */
    public static String readPdf(InputStream is) {
        String result = null;
        PDDocument document = null;
        try {
            PDFParser parser = new PDFParser(new RandomAccessBuffer(is));
            parser.parse();
            document = parser.getPDDocument();
            PDFTextStripper stripper = new PDFTextStripper();
            result = stripper.getText(document);
        } catch (Exception e) {
            log.error("解析pdf文件失败", e);
            throw new BusinessException("解析简历文件失败，请重试");
        }finally {
            try {
                assert document != null;
                document.close();
                is.close();
            } catch (IOException e) {
                log.error("关闭文件失败",e);
            }
        }
        return result;
    }

    public static String getSuffix(String fileName){
        if (StringUtils.isBlank(fileName)){
            return StringUtils.EMPTY;
        }
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }
    public static String parse(MultipartFile file) {
        // 判断文件是pdf还是word
        String filename = file.getOriginalFilename();
        if (StringUtils.isBlank(filename)){
            return StringUtils.EMPTY;
        }
        String suffix = getSuffix(filename);
        try {
            if ("pdf".equals(suffix)) {
                return readPdf(file.getInputStream());
            } else if ("doc".equals(suffix) || "docx".equals(suffix)) {
                return readWord(file.getInputStream());
            } else {
                return StringUtils.EMPTY;
            }
        }catch (Exception e){
            log.error("解析文件失败",e);
            return StringUtils.EMPTY;
        }
    }

    public static boolean isPdfOrWord(String filename){
        String suffix = getSuffix(filename);
        return "pdf".equals(suffix) || "doc".equals(suffix) || "docx".equals(suffix);
    }
}
